import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:jd_shop/utils/ToastMsg.dart';
import 'package:provider/provider.dart';

import '../../event/ProductContentEvent.dart';
import '../../net/UsermsService.dart';
import '../../provider/AppProvider.dart';
import '../../services/ScreenAdaper.dart';
import '../../utils/Storage.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  // 创建 TextEditingController 实例
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _usernameController.text = "18860466733";
    _passwordController.text = "123456";
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text("登录"),
      ),
      body: Container(
        margin: EdgeInsets.only(top: ScreenAdapter.height(50), left: ScreenAdapter.width(20), right: ScreenAdapter.width(20)),
        child: Column(
          children: [
            TextField(
              controller: _usernameController,
              keyboardType: TextInputType.text,
              decoration: const InputDecoration(
                labelText: "用户名",
                hintText: "请输入用户名",
                prefixIcon: Icon(Icons.person),
              ),
              onChanged: (value) {},
            ),
            SizedBox(height: ScreenAdapter.height(20)),
            TextField(
              controller: _passwordController,
              decoration: const InputDecoration(
                labelText: "密码",
                hintText: "请输入密码",
                prefixIcon: Icon(Icons.lock),
              ),
              obscureText: true,
              onChanged: (value) {},
            ),
            InkWell(
              child: Container(
                margin: EdgeInsets.only(top: ScreenAdapter.height(70)),
                width: ScreenAdapter.width(200),
                height: ScreenAdapter.height(40),
                decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.circular(10)),
                alignment: Alignment.center,
                child: Text(
                  "登录",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: ScreenAdapter.fontSize(18)),
                ),
              ),
              onTap: () {
                loginNet();
              },
            ),
          ],
        ),
      ),
    );
  }

  void loginNet() {
    var params = HashMap<String, dynamic>();
    params["userMobile"] = _usernameController.text;
    params["password"] = _passwordController.text;
    UsermsService.loginUser(params).then((value) {
      Storage.setLoginUser(value);
      ToastMsg.show("登录成功");
      Navigator.pop(context);
      eventBus.fire(UserLoginEvent(true));
    });
  }
}
